#!/bin/bash

APP_NAME="ExploitDB"
MAIN_CLASS="xin.ctkqiang.Main"
VERSION="v1.0.0"
RELEASE_DIR="releases/${VERSION}"
OUT_DIR="out"

# 彩色 ASCII 标题（纯 echo）
ascii_banner() {
    echo "   _____            _       _ _   _     _             "
    echo "  | ____|_  ___ __ | | ___ (_) |_(_)___| |_ ___  _ __ "
    echo "  |  _| \ \/ / '_ \| |/ _ \| | __| / __| __/ _ \| '__|"
    echo "  | |___ >  <| |_) | | (_) | | |_| \__ \ || (_) | |   "
    echo "  |_____/_/\_\ .__/|_|\___/|_|\__|_|___/\__\___/|_|   "
    echo "            |_|                                      "
}

# 动画函数
wait_animation() {
    local MESSAGE=$1
    local DOTS=""

    echo "$MESSAGE"
    for i in 1 2 3; do
        DOTS="$DOTS."
        echo "$DOTS"
        sleep 0.3
    done
}

# 步骤开始
ascii_banner
wait_animation "🌸 灵儿正在为你编译 Java 11 项目"

# 清理旧文件
rm -rf $OUT_DIR $RELEASE_DIR
mkdir -p $OUT_DIR

# 编译
find ./src -name "*.java" >sources.txt
javac -source 11 -target 11 -d $OUT_DIR @sources.txt

if [ $? -ne 0 ]; then
    echo "❌ 编译失败了！检查一下语法哦～"
    exit 1
fi

wait_animation "🎀 编译完成，开始打包成 JAR"

# 创建 JAR 包
JAR_NAME="${APP_NAME}-${VERSION}.jar"
jar cfe $JAR_NAME $MAIN_CLASS -C $OUT_DIR .

if [ $? -ne 0 ]; then
    echo "❌ 打包失败了！可能是入口类路径设置错了~"
    exit 1
fi

# 准备 Release 目录
mkdir -p $RELEASE_DIR
mv $JAR_NAME $RELEASE_DIR/

wait_animation "📦 发布结构整理完成"

# Release Summary
echo ""
echo "🎉 Release 打包成功！少女完成度 100%～"
echo "📁 输出目录: $RELEASE_DIR"
echo "📦 文件名: $JAR_NAME"
echo "🔮 Java 版本: $(java -version 2>&1 | head -n 1)"
echo ""
echo "下一步：上传 GitHub Release 页面吧 💖"
